package com.olymtech.jbricks.jbricks.controller;

import com.olymtech.jbricks.jbricks.model.User;
import com.olymtech.jbricks.jbricks.service.CacheDemoService;
import com.olymtech.jbricks.threadpool.DynamicThreadPoolManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池测试
 *
 * @作者 尹吉欢
 * @个人微信 jihuan900
 * @微信公众号 猿天地
 * @GitHub https://github.com/yinjihuan
 * @作者介绍 http://cxytiandi.com/about
 * @时间 2020-07-14 21:42
 */
@RestController
public class TestRestController {

    @Autowired
    private DynamicThreadPoolManager dynamicThreadPoolManager;

    @Autowired
    private CacheDemoService cacheDemoService;

    @GetMapping("/test")
    public String test() {
        ThreadPoolExecutor threadPoolExecutor = dynamicThreadPoolManager.getThreadPoolExecutor("TestThreadPoolExecutor");
        threadPoolExecutor.execute(() -> {
            System.out.println("牛逼的线程池:" + Thread.currentThread().getName());
            try {
                Thread.sleep(5000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        return "success";
    }

    @GetMapping("/cache/{id}")
    public User testCache(@PathVariable("id") Long id) {

        return cacheDemoService.getById(id);
    }

    @GetMapping("/localCache")
    public User testLocalCache() {

        return cacheDemoService.testLocalCache();
    }

}
